package com.test.learnaidl.service;

import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.os.RemoteCallbackList;
import android.os.RemoteException;
import android.text.TextUtils;
import android.util.Log;

import androidx.annotation.Nullable;

import com.test.learnaidl.IOnNewPersonArrivedListener;
import com.test.learnaidl.PersonMannageInterface;
import com.test.learnaidl.bean.PersonBean;

import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;

public class PersonManageService extends Service {
    private static String TAG = "服务端 PersonManageService > ";
    private List<PersonBean> mPersonBeanList = new ArrayList<>();
    /*
        AIDL-接口注册/解注册
        https://www.yuque.com/jiajun-faizt/haag2g/nb3aur
    */
    private RemoteCallbackList<IOnNewPersonArrivedListener> mListener = new RemoteCallbackList<>();

    private Binder mBinder = new PersonMannageInterface.Stub() {
        @Override
        public boolean add(PersonBean personBean) throws RemoteException {
            if (null == personBean) {
                Log.i("==", TAG + "add personBean = null");
                return false;
            }

            if (mPersonBeanList.contains(personBean)) {
                Log.i("==", TAG + "add 失败  mPersonBeanList.contains  = true ,  personBean = "+personBean.toString());
                return false;
            }

            mPersonBeanList.add(personBean);

            //调用接口通知客户端
            if (mListener != null) {
                int size = mListener.beginBroadcast();
                for (int i = 0; i < size; i++) {
                    IOnNewPersonArrivedListener broadcastItem = mListener.getBroadcastItem(i);
                    broadcastItem.onNewsPersonBeanArrived(personBean);
                }

                mListener.finishBroadcast();
            }

            Log.i("==", TAG + "add personBean = " + personBean.toString());

            return true;
        }

        @Override
        public boolean removeById(String id) throws RemoteException {
            if (mPersonBeanList == null || mPersonBeanList.isEmpty()) {
                Log.i("==", TAG + "remove   mPersonBeanList == null 或者 mPersonBeanList.isEmpty() = true   id = " + id);
                return false;
            }

            ListIterator<PersonBean> personBeanListIterator = mPersonBeanList.listIterator();
            while (personBeanListIterator.hasNext()) {
                PersonBean personBean = personBeanListIterator.next();
                if (TextUtils.equals(personBean.getId(),id) ) {

                    boolean removeResult = mPersonBeanList.remove(personBean);

                    if (removeResult) {
                        Log.i("==", TAG + "remove 成功 personBean = " + personBean.toString());

                    } else {
                        Log.i("==", TAG + "remove 失败 id = " + id);
                    }

                    return removeResult;
                }
            }

            Log.i("==", TAG + "remove 失败 id = " + id);
            return false;
        }

        @Override
        public boolean remove(PersonBean personBean) throws RemoteException {
            if (null == personBean) {
                Log.i("==", TAG + "remove personBean = null");
                return false;
            }

            if (mPersonBeanList == null || mPersonBeanList.isEmpty()) {
                Log.i("==", TAG + "remove   mPersonBeanList == null 或者 mPersonBeanList.isEmpty() = true   personBean = " + personBean.toString());
                return false;
            }

            boolean remove = mPersonBeanList.remove(personBean);

            if (remove) {
                Log.i("==", TAG + "remove 成功 personBean = " + personBean.toString());
            }


            return remove;
        }

        @Override
        public PersonBean getPersonBeanById(String id) throws RemoteException {
            if (mPersonBeanList == null || mPersonBeanList.isEmpty()) {
                Log.i("==", TAG + "getPersonBean   mPersonBeanList == null 或者 mPersonBeanList.isEmpty() = true   id = " + id);
                return null;
            }
            for (PersonBean personBean : mPersonBeanList) {
                if (TextUtils.equals(personBean.getId(),id)) {
                    Log.i("==", TAG + "getPersonBean 成功 personBean = " + personBean.toString());

                    return personBean;
                }
            }

            Log.i("==", TAG + "getPersonBean 失败 id = " + id);
            return null;
        }

        @Override
        public List<PersonBean> getPersonBeans() throws RemoteException {
            if (mPersonBeanList == null || mPersonBeanList.isEmpty()) {
                Log.i("==", TAG + " 获取全部 getPersonBeans   mPersonBeanList == null 或者 mPersonBeanList.isEmpty() = true ");
            }
            return mPersonBeanList;
        }

        @Override
        public void registerListener(IOnNewPersonArrivedListener listener) throws RemoteException {
            mListener.register(listener);
            Log.i("==", TAG + "registerListener ");

        }

        @Override
        public void unregisterListener(IOnNewPersonArrivedListener listener) throws RemoteException {
            mListener.unregister(listener);
            Log.i("==", TAG + "unregisterListener ");

        }
    };

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return mBinder;
    }

    @Override
    public void onCreate() {
        super.onCreate();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public void onRebind(Intent intent) {
        super.onRebind(intent);
    }

    @Override
    public boolean onUnbind(Intent intent) {
        return super.onUnbind(intent);
    }


    @Override
    public void onDestroy() {
        super.onDestroy();
    }

}
